home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / string / nsStringAPI.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  57KB  |  1,738 lines

  1. /* vim:set ts=2 sw=2 et cindent: */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Mozilla.
  16.  *
  17.  * The Initial Developer of the Original Code is IBM Corporation.
  18.  * Portions created by IBM Corporation are Copyright (C) 2003
  19.  * IBM Corporation.  All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Darin Fisher <darin@meer.net>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. #ifndef nsStringAPI_h__
  39. #define nsStringAPI_h__
  40.  
  41. #include <string.h>
  42.  
  43. /**
  44.  * nsStringAPI.h
  45.  *
  46.  * This file describes a minimal API for working with XPCOM's abstract
  47.  * string classes.  It divorces the consumer from having any run-time
  48.  * dependency on the implementation details of the abstract string types.
  49.  */
  50.  
  51. // Map frozen functions to private symbol names if not using strict API.
  52. #ifdef MOZILLA_INTERNAL_API
  53. # define NS_StringContainerInit           NS_StringContainerInit_P
  54. # define NS_StringContainerInit2          NS_StringContainerInit2_P
  55. # define NS_StringContainerFinish         NS_StringContainerFinish_P
  56. # define NS_StringGetData                 NS_StringGetData_P
  57. # define NS_StringGetMutableData          NS_StringGetMutableData_P
  58. # define NS_StringCloneData               NS_StringCloneData_P
  59. # define NS_StringSetData                 NS_StringSetData_P
  60. # define NS_StringSetDataRange            NS_StringSetDataRange_P
  61. # define NS_StringCopy                    NS_StringCopy_P
  62. # define NS_CStringContainerInit          NS_CStringContainerInit_P
  63. # define NS_CStringContainerInit2         NS_CStringContainerInit2_P
  64. # define NS_CStringContainerFinish        NS_CStringContainerFinish_P
  65. # define NS_CStringGetData                NS_CStringGetData_P
  66. # define NS_CStringGetMutableData         NS_CStringGetMutableData_P
  67. # define NS_CStringCloneData              NS_CStringCloneData_P
  68. # define NS_CStringSetData                NS_CStringSetData_P
  69. # define NS_CStringSetDataRange           NS_CStringSetDataRange_P
  70. # define NS_CStringCopy                   NS_CStringCopy_P
  71. # define NS_CStringToUTF16                NS_CStringToUTF16_P
  72. # define NS_UTF16ToCString                NS_UTF16ToCString_P
  73. #endif
  74.  
  75. #include "nscore.h"
  76.  
  77. #if defined( XPCOM_GLUE )
  78. #define NS_STRINGAPI(type) extern "C" NS_HIDDEN_(type)
  79. #elif defined( _IMPL_NS_STRINGAPI )
  80. #define NS_STRINGAPI(type) extern "C" NS_EXPORT type
  81. #else
  82. #define NS_STRINGAPI(type) extern "C" NS_IMPORT type
  83. #endif
  84.  
  85. /* The base string types */
  86. class nsAString;
  87. class nsACString;
  88.  
  89. /* ------------------------------------------------------------------------- */
  90.  
  91. /**
  92.  * nsStringContainer
  93.  *
  94.  * This is an opaque data type that is large enough to hold the canonical
  95.  * implementation of nsAString.  The binary structure of this class is an
  96.  * implementation detail.
  97.  *
  98.  * The string data stored in a string container is always single fragment
  99.  * and may be null-terminated depending on how it is initialized.
  100.  *
  101.  * Typically, string containers are allocated on the stack for temporary
  102.  * use.  However, they can also be malloc'd if necessary.  In either case,
  103.  * a string container is not useful until it has been initialized with a
  104.  * call to NS_StringContainerInit.  The following example shows how to use
  105.  * a string container to call a function that takes a |nsAString &| out-param.
  106.  *
  107.  *   NS_METHOD GetBlah(nsAString &aBlah);
  108.  *
  109.  *   nsresult MyCode()
  110.  *   {
  111.  *     nsresult rv;
  112.  *
  113.  *     nsStringContainer sc;
  114.  *     rv = NS_StringContainerInit(sc);
  115.  *     if (NS_FAILED(rv))
  116.  *       return rv;
  117.  *
  118.  *     rv = GetBlah(sc);
  119.  *     if (NS_SUCCEEDED(rv))
  120.  *     {
  121.  *       const PRUnichar *data;
  122.  *       NS_StringGetData(sc, &data);
  123.  *       //
  124.  *       // |data| now points to the result of the GetBlah function
  125.  *       //
  126.  *     }
  127.  *
  128.  *     NS_StringContainerFinish(sc);
  129.  *     return rv;
  130.  *   }
  131.  *
  132.  * The following example show how to use a string container to pass a string
  133.  * parameter to a function taking a |const nsAString &| in-param.
  134.  *
  135.  *   NS_METHOD SetBlah(const nsAString &aBlah);
  136.  *
  137.  *   nsresult MyCode()
  138.  *   {
  139.  *     nsresult rv;
  140.  *
  141.  *     nsStringContainer sc;
  142.  *     rv = NS_StringContainerInit(sc);
  143.  *     if (NS_FAILED(rv))
  144.  *       return rv;
  145.  *
  146.  *     const PRUnichar kData[] = {'x','y','z','\0'};
  147.  *     rv = NS_StringSetData(sc, kData, sizeof(kData)/2 - 1);
  148.  *     if (NS_SUCCEEDED(rv))
  149.  *       rv = SetBlah(sc);
  150.  *
  151.  *     NS_StringContainerFinish(sc);
  152.  *     return rv;
  153.  *   }
  154.  */
  155. class nsStringContainer;
  156.  
  157. /**
  158.  * Flags that may be OR'd together to pass to NS_StringContainerInit2:
  159.  */
  160. enum {
  161.   /* Data passed into NS_StringContainerInit2 is not copied; instead, the
  162.    * string references the passed in data pointer directly.  The caller must
  163.    * ensure that the data is valid for the lifetime of the string container.
  164.    * This flag should not be combined with NS_STRING_CONTAINER_INIT_ADOPT. */
  165.   NS_STRING_CONTAINER_INIT_DEPEND    = (1 << 1),
  166.  
  167.   /* Data passed into NS_StringContainerInit2 is not copied; instead, the
  168.    * string takes ownership over the data pointer.  The caller must have
  169.    * allocated the data array using the XPCOM memory allocator (nsMemory).
  170.    * This flag should not be combined with NS_STRING_CONTAINER_INIT_DEPEND. */
  171.   NS_STRING_CONTAINER_INIT_ADOPT     = (1 << 2),
  172.  
  173.   /* Data passed into NS_StringContainerInit2 is a substring that is not
  174.    * null-terminated. */
  175.   NS_STRING_CONTAINER_INIT_SUBSTRING = (1 << 3)
  176. };
  177.  
  178. /**
  179.  * NS_StringContainerInit
  180.  *
  181.  * @param aContainer    string container reference
  182.  * @return              NS_OK if string container successfully initialized
  183.  *
  184.  * This function may allocate additional memory for aContainer.  When
  185.  * aContainer is no longer needed, NS_StringContainerFinish should be called.
  186.  *
  187.  * @status FROZEN
  188.  */
  189. NS_STRINGAPI(nsresult)
  190. NS_StringContainerInit(nsStringContainer &aContainer);
  191.  
  192. /**
  193.  * NS_StringContainerInit2
  194.  *
  195.  * @param aContainer    string container reference
  196.  * @param aData         character buffer (may be null)
  197.  * @param aDataLength   number of characters stored at aData (may pass
  198.  *                      PR_UINT32_MAX if aData is null-terminated)
  199.  * @param aFlags        flags affecting how the string container is
  200.  *                      initialized.  this parameter is ignored when aData
  201.  *                      is null.  otherwise, if this parameter is 0, then
  202.  *                      aData is copied into the string.
  203.  *
  204.  * This function resembles NS_StringContainerInit but provides further
  205.  * options that permit more efficient memory usage.  When aContainer is
  206.  * no longer needed, NS_StringContainerFinish should be called.
  207.  *
  208.  * NOTE: NS_StringContainerInit2(container, nsnull, 0, 0) is equivalent to
  209.  * NS_StringContainerInit(container).
  210.  *
  211.  * @status FROZEN
  212.  */
  213. NS_STRINGAPI(nsresult)
  214. NS_StringContainerInit2
  215.   (nsStringContainer &aContainer, const PRUnichar *aData = nsnull,
  216.    PRUint32 aDataLength = PR_UINT32_MAX, PRUint32 aFlags = 0);
  217.  
  218. /**
  219.  * NS_StringContainerFinish
  220.  *
  221.  * @param aContainer    string container reference
  222.  *
  223.  * This function frees any memory owned by aContainer.
  224.  *
  225.  * @status FROZEN
  226.  */
  227. NS_STRINGAPI(void)
  228. NS_StringContainerFinish(nsStringContainer &aContainer);
  229.  
  230. /* ------------------------------------------------------------------------- */
  231.  
  232. /**
  233.  * NS_StringGetData
  234.  *
  235.  * This function returns a const character pointer to the string's internal
  236.  * buffer, the length of the string, and a boolean value indicating whether
  237.  * or not the buffer is null-terminated.
  238.  *
  239.  * @param aStr          abstract string reference
  240.  * @param aData         out param that will hold the address of aStr's
  241.  *                      internal buffer
  242.  * @param aTerminated   if non-null, this out param will be set to indicate
  243.  *                      whether or not aStr's internal buffer is null-
  244.  *                      terminated
  245.  * @return              length of aStr's internal buffer
  246.  *
  247.  * @status FROZEN
  248.  */
  249. NS_STRINGAPI(PRUint32)
  250. NS_StringGetData
  251.   (const nsAString &aStr, const PRUnichar **aData,
  252.    PRBool *aTerminated = nsnull);
  253.  
  254. /**
  255.  * NS_StringGetMutableData
  256.  *
  257.  * This function provides mutable access to a string's internal buffer.  It
  258.  * returns a pointer to an array of characters that may be modified.  The
  259.  * returned pointer remains valid until the string object is passed to some
  260.  * other string function.
  261.  *
  262.  * Optionally, this function may be used to resize the string's internal
  263.  * buffer.  The aDataLength parameter specifies the requested length of the
  264.  * string's internal buffer.  By passing some value other than PR_UINT32_MAX,
  265.  * the caller can request that the buffer be resized to the specified number of
  266.  * characters before returning.  The caller is not responsible for writing a
  267.  * null-terminator.
  268.  *
  269.  * @param aStr          abstract string reference
  270.  * @param aDataLength   number of characters to resize the string's internal
  271.  *                      buffer to or PR_UINT32_MAX if no resizing is needed
  272.  * @param aData         out param that upon return holds the address of aStr's
  273.  *                      internal buffer or null if the function failed
  274.  * @return              number of characters or zero if the function failed
  275.  *
  276.  * This function does not necessarily null-terminate aStr after resizing its
  277.  * internal buffer.  The behavior depends on the implementation of the abstract
  278.  * string, aStr.  If aStr is a reference to a nsStringContainer, then its data
  279.  * will be null-terminated by this function.
  280.  *
  281.  * @status FROZEN
  282.  */
  283. NS_STRINGAPI(PRUint32)
  284. NS_StringGetMutableData
  285.   (nsAString &aStr, PRUint32 aDataLength, PRUnichar **aData);
  286.  
  287. /**
  288.  * NS_StringCloneData
  289.  *
  290.  * This function returns a null-terminated copy of the string's
  291.  * internal buffer.
  292.  *
  293.  * @param aStr          abstract string reference
  294.  * @return              null-terminated copy of the string's internal buffer
  295.  *                      (it must be free'd using using nsMemory::Free)
  296.  *
  297.  * @status FROZEN
  298.  */
  299. NS_STRINGAPI(PRUnichar *)
  300. NS_StringCloneData
  301.   (const nsAString &aStr);
  302.  
  303. /**
  304.  * NS_StringSetData
  305.  *
  306.  * This function copies aData into aStr.
  307.  *
  308.  * @param aStr          abstract string reference
  309.  * @param aData         character buffer
  310.  * @param aDataLength   number of characters to copy from source string (pass
  311.  *                      PR_UINT32_MAX to copy until end of aData, designated by
  312.  *                      a null character)
  313.  * @return              NS_OK if function succeeded
  314.  *
  315.  * This function does not necessarily null-terminate aStr after copying data
  316.  * from aData.  The behavior depends on the implementation of the abstract
  317.  * string, aStr.  If aStr is a reference to a nsStringContainer, then its data
  318.  * will be null-terminated by this function.
  319.  *
  320.  * @status FROZEN
  321.  */
  322. NS_STRINGAPI(nsresult)
  323. NS_StringSetData
  324.   (nsAString &aStr, const PRUnichar *aData,
  325.    PRUint32 aDataLength = PR_UINT32_MAX);
  326.  
  327. /**
  328.  * NS_StringSetDataRange
  329.  *
  330.  * This function copies aData into a section of aStr.  As a result it can be
  331.  * used to insert new characters into the string.
  332.  *
  333.  * @param aStr          abstract string reference
  334.  * @param aCutOffset    starting index where the string's existing data
  335.  *                      is to be overwritten (pass PR_UINT32_MAX to cause
  336.  *                      aData to be appended to the end of aStr, in which
  337.  *                      case the value of aCutLength is ignored).
  338.  * @param aCutLength    number of characters to overwrite starting at
  339.  *                      aCutOffset (pass PR_UINT32_MAX to overwrite until the
  340.  *                      end of aStr).
  341.  * @param aData         character buffer (pass null to cause this function
  342.  *                      to simply remove the "cut" range)
  343.  * @param aDataLength   number of characters to copy from source string (pass
  344.  *                      PR_UINT32_MAX to copy until end of aData, designated by
  345.  *                      a null character)
  346.  * @return              NS_OK if function succeeded
  347.  *
  348.  * This function does not necessarily null-terminate aStr after copying data
  349.  * from aData.  The behavior depends on the implementation of the abstract
  350.  * string, aStr.  If aStr is a reference to a nsStringContainer, then its data
  351.  * will be null-terminated by this function.
  352.  *
  353.  * @status FROZEN
  354.  */
  355. NS_STRINGAPI(nsresult)
  356. NS_StringSetDataRange
  357.   (nsAString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength,
  358.    const PRUnichar *aData, PRUint32 aDataLength = PR_UINT32_MAX);
  359.  
  360. /**
  361.  * NS_StringCopy
  362.  *
  363.  * This function makes aDestStr have the same value as aSrcStr.  It is
  364.  * provided as an optimization.
  365.  *
  366.  * @param aDestStr      abstract string reference to be modified
  367.  * @param aSrcStr       abstract string reference containing source string
  368.  * @return              NS_OK if function succeeded
  369.  *
  370.  * This function does not necessarily null-terminate aDestStr after copying
  371.  * data from aSrcStr.  The behavior depends on the implementation of the
  372.  * abstract string, aDestStr.  If aDestStr is a reference to a
  373.  * nsStringContainer, then its data will be null-terminated by this function.
  374.  *
  375.  * @status FROZEN
  376.  */
  377. NS_STRINGAPI(nsresult)
  378. NS_StringCopy
  379.   (nsAString &aDestStr, const nsAString &aSrcStr);
  380.  
  381. /**
  382.  * NS_StringAppendData
  383.  *
  384.  * This function appends data to the existing value of aStr.
  385.  *
  386.  * @param aStr          abstract string reference to be modified
  387.  * @param aData         character buffer
  388.  * @param aDataLength   number of characters to append (pass PR_UINT32_MAX to
  389.  *                      append until a null-character is encountered)
  390.  * @return              NS_OK if function succeeded
  391.  *
  392.  * This function does not necessarily null-terminate aStr upon completion.
  393.  * The behavior depends on the implementation of the abstract string, aStr.
  394.  * If aStr is a reference to a nsStringContainer, then its data will be null-
  395.  * terminated by this function.
  396.  */
  397. inline NS_HIDDEN_(nsresult)
  398. NS_StringAppendData(nsAString &aStr, const PRUnichar *aData,
  399.                     PRUint32 aDataLength = PR_UINT32_MAX)
  400. {
  401.   return NS_StringSetDataRange(aStr, PR_UINT32_MAX, 0, aData, aDataLength);
  402. }
  403.  
  404. /**
  405.  * NS_StringInsertData
  406.  *
  407.  * This function inserts data into the existing value of aStr at the specified
  408.  * offset.
  409.  *
  410.  * @param aStr          abstract string reference to be modified
  411.  * @param aOffset       specifies where in the string to insert aData
  412.  * @param aData         character buffer
  413.  * @param aDataLength   number of characters to append (pass PR_UINT32_MAX to
  414.  *                      append until a null-character is encountered)
  415.  * @return              NS_OK if function succeeded
  416.  *
  417.  * This function does not necessarily null-terminate aStr upon completion.
  418.  * The behavior depends on the implementation of the abstract string, aStr.
  419.  * If aStr is a reference to a nsStringContainer, then its data will be null-
  420.  * terminated by this function.
  421.  */
  422. inline NS_HIDDEN_(nsresult)
  423. NS_StringInsertData(nsAString &aStr, PRUint32 aOffset, const PRUnichar *aData,
  424.                     PRUint32 aDataLength = PR_UINT32_MAX)
  425. {
  426.   return NS_StringSetDataRange(aStr, aOffset, 0, aData, aDataLength);
  427. }
  428.  
  429. /**
  430.  * NS_StringCutData
  431.  *
  432.  * This function shortens the existing value of aStr, by removing characters
  433.  * at the specified offset.
  434.  *
  435.  * @param aStr          abstract string reference to be modified
  436.  * @param aCutOffset    specifies where in the string to insert aData
  437.  * @param aCutLength    number of characters to remove
  438.  * @return              NS_OK if function succeeded
  439.  */
  440. inline NS_HIDDEN_(nsresult)
  441. NS_StringCutData(nsAString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength)
  442. {
  443.   return NS_StringSetDataRange(aStr, aCutOffset, aCutLength, nsnull, 0);
  444. }
  445.  
  446. /* ------------------------------------------------------------------------- */
  447.  
  448. /**
  449.  * nsCStringContainer
  450.  *
  451.  * This is an opaque data type that is large enough to hold the canonical
  452.  * implementation of nsACString.  The binary structure of this class is an
  453.  * implementation detail.
  454.  *
  455.  * The string data stored in a string container is always single fragment
  456.  * and may be null-terminated depending on how it is initialized.
  457.  *
  458.  * @see nsStringContainer for use cases and further documentation.
  459.  */
  460. class nsCStringContainer;
  461.  
  462. /**
  463.  * Flags that may be OR'd together to pass to NS_StringContainerInit2:
  464.  */
  465. enum {
  466.   /* Data passed into NS_CStringContainerInit2 is not copied; instead, the
  467.    * string references the passed in data pointer directly.  The caller must
  468.    * ensure that the data is valid for the lifetime of the string container.
  469.    * This flag should not be combined with NS_CSTRING_CONTAINER_INIT_ADOPT. */
  470.   NS_CSTRING_CONTAINER_INIT_DEPEND    = (1 << 1),
  471.  
  472.   /* Data passed into NS_CStringContainerInit2 is not copied; instead, the
  473.    * string takes ownership over the data pointer.  The caller must have
  474.    * allocated the data array using the XPCOM memory allocator (nsMemory).
  475.    * This flag should not be combined with NS_CSTRING_CONTAINER_INIT_DEPEND. */
  476.   NS_CSTRING_CONTAINER_INIT_ADOPT     = (1 << 2),
  477.  
  478.   /* Data passed into NS_CStringContainerInit2 is a substring that is not
  479.    * null-terminated. */
  480.   NS_CSTRING_CONTAINER_INIT_SUBSTRING = (1 << 3)
  481. };
  482.  
  483. /**
  484.  * NS_CStringContainerInit
  485.  *
  486.  * @param aContainer    string container reference
  487.  * @return              NS_OK if string container successfully initialized
  488.  *
  489.  * This function may allocate additional memory for aContainer.  When
  490.  * aContainer is no longer needed, NS_CStringContainerFinish should be called.
  491.  *
  492.  * @status FROZEN
  493.  */
  494. NS_STRINGAPI(nsresult)
  495. NS_CStringContainerInit(nsCStringContainer &aContainer);
  496.  
  497. /**
  498.  * NS_CStringContainerInit2
  499.  *
  500.  * @param aContainer    string container reference
  501.  * @param aData         character buffer (may be null)
  502.  * @param aDataLength   number of characters stored at aData (may pass
  503.  *                      PR_UINT32_MAX if aData is null-terminated)
  504.  * @param aFlags        flags affecting how the string container is
  505.  *                      initialized.  this parameter is ignored when aData
  506.  *                      is null.  otherwise, if this parameter is 0, then
  507.  *                      aData is copied into the string.
  508.  *
  509.  * This function resembles NS_CStringContainerInit but provides further
  510.  * options that permit more efficient memory usage.  When aContainer is
  511.  * no longer needed, NS_CStringContainerFinish should be called.
  512.  *
  513.  * NOTE: NS_CStringContainerInit2(container, nsnull, 0, 0) is equivalent to
  514.  * NS_CStringContainerInit(container).
  515.  *
  516.  * @status FROZEN
  517.  */
  518. NS_STRINGAPI(nsresult)
  519. NS_CStringContainerInit2
  520.   (nsCStringContainer &aContainer, const char *aData = nsnull,
  521.    PRUint32 aDataLength = PR_UINT32_MAX, PRUint32 aFlags = 0);
  522.  
  523. /**
  524.  * NS_CStringContainerFinish
  525.  *
  526.  * @param aContainer    string container reference
  527.  *
  528.  * This function frees any memory owned by aContainer.
  529.  *
  530.  * @status FROZEN
  531.  */
  532. NS_STRINGAPI(void)
  533. NS_CStringContainerFinish(nsCStringContainer &aContainer);
  534.  
  535. /* ------------------------------------------------------------------------- */
  536.  
  537. /**
  538.  * NS_CStringGetData
  539.  *
  540.  * This function returns a const character pointer to the string's internal
  541.  * buffer, the length of the string, and a boolean value indicating whether
  542.  * or not the buffer is null-terminated.
  543.  *
  544.  * @param aStr          abstract string reference
  545.  * @param aData         out param that will hold the address of aStr's
  546.  *                      internal buffer
  547.  * @param aTerminated   if non-null, this out param will be set to indicate
  548.  *                      whether or not aStr's internal buffer is null-
  549.  *                      terminated
  550.  * @return              length of aStr's internal buffer
  551.  *
  552.  * @status FROZEN
  553.  */
  554. NS_STRINGAPI(PRUint32)
  555. NS_CStringGetData
  556.   (const nsACString &aStr, const char **aData,
  557.    PRBool *aTerminated = nsnull);
  558.  
  559. /**
  560.  * NS_CStringGetMutableData
  561.  *
  562.  * This function provides mutable access to a string's internal buffer.  It
  563.  * returns a pointer to an array of characters that may be modified.  The
  564.  * returned pointer remains valid until the string object is passed to some
  565.  * other string function.
  566.  *
  567.  * Optionally, this function may be used to resize the string's internal
  568.  * buffer.  The aDataLength parameter specifies the requested length of the
  569.  * string's internal buffer.  By passing some value other than PR_UINT32_MAX,
  570.  * the caller can request that the buffer be resized to the specified number of
  571.  * characters before returning.  The caller is not responsible for writing a
  572.  * null-terminator.
  573.  *
  574.  * @param aStr          abstract string reference
  575.  * @param aDataLength   number of characters to resize the string's internal
  576.  *                      buffer to or PR_UINT32_MAX if no resizing is needed
  577.  * @param aData         out param that upon return holds the address of aStr's
  578.  *                      internal buffer or null if the function failed
  579.  * @return              number of characters or zero if the function failed
  580.  *
  581.  * This function does not necessarily null-terminate aStr after resizing its
  582.  * internal buffer.  The behavior depends on the implementation of the abstract
  583.  * string, aStr.  If aStr is a reference to a nsStringContainer, then its data
  584.  * will be null-terminated by this function.
  585.  *
  586.  * @status FROZEN
  587.  */
  588. NS_STRINGAPI(PRUint32)
  589. NS_CStringGetMutableData
  590.   (nsACString &aStr, PRUint32 aDataLength, char **aData);
  591.  
  592. /**
  593.  * NS_CStringCloneData
  594.  *
  595.  * This function returns a null-terminated copy of the string's
  596.  * internal buffer.
  597.  *
  598.  * @param aStr          abstract string reference
  599.  * @return              null-terminated copy of the string's internal buffer
  600.  *                      (it must be free'd using using nsMemory::Free)
  601.  *
  602.  * @status FROZEN
  603.  */
  604. NS_STRINGAPI(char *)
  605. NS_CStringCloneData
  606.   (const nsACString &aStr);
  607.  
  608. /**
  609.  * NS_CStringSetData
  610.  *
  611.  * This function copies aData into aStr.
  612.  *
  613.  * @param aStr          abstract string reference
  614.  * @param aData         character buffer
  615.  * @param aDataLength   number of characters to copy from source string (pass
  616.  *                      PR_UINT32_MAX to copy until end of aData, designated by
  617.  *                      a null character)
  618.  * @return              NS_OK if function succeeded
  619.  *
  620.  * This function does not necessarily null-terminate aStr after copying data
  621.  * from aData.  The behavior depends on the implementation of the abstract
  622.  * string, aStr.  If aStr is a reference to a nsStringContainer, then its data
  623.  * will be null-terminated by this function.
  624.  *
  625.  * @status FROZEN
  626.  */
  627. NS_STRINGAPI(nsresult)
  628. NS_CStringSetData
  629.   (nsACString &aStr, const char *aData,
  630.    PRUint32 aDataLength = PR_UINT32_MAX);
  631.  
  632. /**
  633.  * NS_CStringSetDataRange
  634.  *
  635.  * This function copies aData into a section of aStr.  As a result it can be
  636.  * used to insert new characters into the string.
  637.  *
  638.  * @param aStr          abstract string reference
  639.  * @param aCutOffset    starting index where the string's existing data
  640.  *                      is to be overwritten (pass PR_UINT32_MAX to cause
  641.  *                      aData to be appended to the end of aStr, in which
  642.  *                      case the value of aCutLength is ignored).
  643.  * @param aCutLength    number of characters to overwrite starting at
  644.  *                      aCutOffset (pass PR_UINT32_MAX to overwrite until the
  645.  *                      end of aStr).
  646.  * @param aData         character buffer (pass null to cause this function
  647.  *                      to simply remove the "cut" range)
  648.  * @param aDataLength   number of characters to copy from source string (pass
  649.  *                      PR_UINT32_MAX to copy until end of aData, designated by
  650.  *                      a null character)
  651.  * @return              NS_OK if function succeeded
  652.  *
  653.  * This function does not necessarily null-terminate aStr after copying data
  654.  * from aData.  The behavior depends on the implementation of the abstract
  655.  * string, aStr.  If aStr is a reference to a nsStringContainer, then its data
  656.  * will be null-terminated by this function.
  657.  *
  658.  * @status FROZEN
  659.  */
  660. NS_STRINGAPI(nsresult)
  661. NS_CStringSetDataRange
  662.   (nsACString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength,
  663.    const char *aData, PRUint32 aDataLength = PR_UINT32_MAX);
  664.  
  665. /**
  666.  * NS_CStringCopy
  667.  *
  668.  * This function makes aDestStr have the same value as aSrcStr.  It is
  669.  * provided as an optimization.
  670.  *
  671.  * @param aDestStr      abstract string reference to be modified
  672.  * @param aSrcStr       abstract string reference containing source string
  673.  * @return              NS_OK if function succeeded
  674.  *
  675.  * This function does not necessarily null-terminate aDestStr after copying
  676.  * data from aSrcStr.  The behavior depends on the implementation of the
  677.  * abstract string, aDestStr.  If aDestStr is a reference to a
  678.  * nsStringContainer, then its data will be null-terminated by this function.
  679.  *
  680.  * @status FROZEN
  681.  */
  682. NS_STRINGAPI(nsresult)
  683. NS_CStringCopy
  684.   (nsACString &aDestStr, const nsACString &aSrcStr);
  685.  
  686. /**
  687.  * NS_CStringAppendData
  688.  *
  689.  * This function appends data to the existing value of aStr.
  690.  *
  691.  * @param aStr          abstract string reference to be modified
  692.  * @param aData         character buffer
  693.  * @param aDataLength   number of characters to append (pass PR_UINT32_MAX to
  694.  *                      append until a null-character is encountered)
  695.  * @return              NS_OK if function succeeded
  696.  *
  697.  * This function does not necessarily null-terminate aStr upon completion.
  698.  * The behavior depends on the implementation of the abstract string, aStr.
  699.  * If aStr is a reference to a nsStringContainer, then its data will be null-
  700.  * terminated by this function.
  701.  */
  702. inline NS_HIDDEN_(nsresult)
  703. NS_CStringAppendData(nsACString &aStr, const char *aData,
  704.                     PRUint32 aDataLength = PR_UINT32_MAX)
  705. {
  706.   return NS_CStringSetDataRange(aStr, PR_UINT32_MAX, 0, aData, aDataLength);
  707. }
  708.  
  709. /**
  710.  * NS_CStringInsertData
  711.  *
  712.  * This function inserts data into the existing value of aStr at the specified
  713.  * offset.
  714.  *
  715.  * @param aStr          abstract string reference to be modified
  716.  * @param aOffset       specifies where in the string to insert aData
  717.  * @param aData         character buffer
  718.  * @param aDataLength   number of characters to append (pass PR_UINT32_MAX to
  719.  *                      append until a null-character is encountered)
  720.  * @return              NS_OK if function succeeded
  721.  *
  722.  * This function does not necessarily null-terminate aStr upon completion.
  723.  * The behavior depends on the implementation of the abstract string, aStr.
  724.  * If aStr is a reference to a nsStringContainer, then its data will be null-
  725.  * terminated by this function.
  726.  */
  727. inline NS_HIDDEN_(nsresult)
  728. NS_CStringInsertData(nsACString &aStr, PRUint32 aOffset, const char *aData,
  729.                     PRUint32 aDataLength = PR_UINT32_MAX)
  730. {
  731.   return NS_CStringSetDataRange(aStr, aOffset, 0, aData, aDataLength);
  732. }
  733.  
  734. /**
  735.  * NS_CStringCutData
  736.  *
  737.  * This function shortens the existing value of aStr, by removing characters
  738.  * at the specified offset.
  739.  *
  740.  * @param aStr          abstract string reference to be modified
  741.  * @param aCutOffset    specifies where in the string to insert aData
  742.  * @param aCutLength    number of characters to remove
  743.  * @return              NS_OK if function succeeded
  744.  */
  745. inline NS_HIDDEN_(nsresult)
  746. NS_CStringCutData(nsACString &aStr, PRUint32 aCutOffset, PRUint32 aCutLength)
  747. {
  748.   return NS_CStringSetDataRange(aStr, aCutOffset, aCutLength, nsnull, 0);
  749. }
  750.  
  751. /* ------------------------------------------------------------------------- */
  752.  
  753. /**
  754.  * Encodings that can be used with the following conversion routines.
  755.  */
  756. enum nsCStringEncoding {
  757.   /* Conversion between ASCII and UTF-16 assumes that all bytes in the source
  758.    * string are 7-bit ASCII and can be inflated to UTF-16 by inserting null
  759.    * bytes.  Reverse conversion is done by truncating every other byte.  The
  760.    * conversion may result in loss and/or corruption of information if the
  761.    * strings do not strictly contain ASCII data. */
  762.   NS_CSTRING_ENCODING_ASCII = 0,
  763.  
  764.   /* Conversion between UTF-8 and UTF-16 is non-lossy. */
  765.   NS_CSTRING_ENCODING_UTF8 = 1,
  766.  
  767.   /* Conversion from UTF-16 to the native filesystem charset may result in a
  768.    * loss of information.  No attempt is made to protect against data loss in
  769.    * this case.  The native filesystem charset applies to strings passed to
  770.    * the "Native" method variants on nsIFile and nsILocalFile. */
  771.   NS_CSTRING_ENCODING_NATIVE_FILESYSTEM = 2
  772. };
  773.  
  774. /**
  775.  * NS_CStringToUTF16
  776.  *
  777.  * This function converts the characters in a nsACString to an array of UTF-16
  778.  * characters, in the platform endianness.  The result is stored in a nsAString
  779.  * object.
  780.  *
  781.  * @param aSource       abstract string reference containing source string
  782.  * @param aSrcEncoding  character encoding of the source string
  783.  * @param aDest         abstract string reference to hold the result
  784.  *
  785.  * @status FROZEN
  786.  */
  787. NS_STRINGAPI(nsresult)
  788. NS_CStringToUTF16(const nsACString &aSource, nsCStringEncoding aSrcEncoding,
  789.                   nsAString &aDest);
  790.  
  791. /**
  792.  * NS_UTF16ToCString
  793.  *
  794.  * This function converts the UTF-16 characters in a nsAString to a single-byte
  795.  * encoding.  The result is stored in a nsACString object.  In some cases this
  796.  * conversion may be lossy.  In such cases, the conversion may succeed with a
  797.  * return code indicating loss of information.  The exact behavior is not
  798.  * specified at this time.
  799.  *
  800.  * @param aSource       abstract string reference containing source string
  801.  * @param aDestEncoding character encoding of the resulting string
  802.  * @param aDest         abstract string reference to hold the result
  803.  *
  804.  * @status FROZEN
  805.  */
  806. NS_STRINGAPI(nsresult)
  807. NS_UTF16ToCString(const nsAString &aSource, nsCStringEncoding aDestEncoding,
  808.                   nsACString &aDest);
  809.  
  810. /* ------------------------------------------------------------------------- */
  811.  
  812. /**
  813.  * Below we define nsAString and nsACString.  The "_external" suffix is an
  814.  * implementation detail.  nsAString_external is the name of the external
  815.  * representation of nsAString from the point of view of the Mozilla codebase.
  816.  * To a user of this API, nsAString_external is exactly nsAString.
  817.  *
  818.  * These classes should be treated as abstract classes with unspecified
  819.  * structure.  The inline methods are provided as helper functions around the
  820.  * C-style API provided above.
  821.  *
  822.  * Do not try to mix these definitions of nsAString and nsACString with the
  823.  * internal definition of these classes from nsAString.h in the Mozilla tree.
  824.  */
  825.  
  826. #ifndef MOZILLA_INTERNAL_API
  827. #define nsAString_external nsAString
  828. #define nsACString_external nsACString
  829. #endif
  830.  
  831. class nsAString_external
  832. {
  833. #ifndef MOZILLA_INTERNAL_API
  834.  
  835. public:
  836.   typedef PRUnichar             char_type;
  837.   typedef nsAString_external    self_type;
  838.   typedef PRUint32              size_type;
  839.   typedef PRUint32              index_type;
  840.  
  841.   NS_HIDDEN_(const char_type*) BeginReading() const
  842.   {
  843.     const char_type *data;
  844.     NS_StringGetData(*this, &data);
  845.     return data;
  846.   }
  847.  
  848.   NS_HIDDEN_(const char_type*) EndReading() const
  849.   {
  850.     const char_type *data;
  851.     PRUint32 len = NS_StringGetData(*this, &data);
  852.     return data + len;
  853.   }
  854.  
  855.   NS_HIDDEN_(char_type*) BeginWriting()
  856.   {
  857.     char_type *data;
  858.     NS_StringGetMutableData(*this, PR_UINT32_MAX, &data);
  859.     return data;
  860.   }
  861.  
  862.   NS_HIDDEN_(PRBool) SetLength(PRUint32 aLen)
  863.   {
  864.     char_type *data;
  865.     NS_StringGetMutableData(*this, aLen, &data);
  866.     return data != nsnull;
  867.   }
  868.  
  869.   NS_HIDDEN_(size_type) Length() const
  870.   {
  871.     const char_type* data;
  872.     return NS_StringGetData(*this, &data);
  873.   }
  874.  
  875.   NS_HIDDEN_(PRBool) IsEmpty() const
  876.   {
  877.     return Length() == 0;
  878.   }
  879.  
  880.   NS_HIDDEN_(void) Assign(const self_type& aString)
  881.   {
  882.     NS_StringCopy(*this, aString);
  883.   }
  884.   NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = PR_UINT32_MAX)
  885.   {
  886.     NS_StringSetData(*this, aData, aLength);
  887.   }
  888.   NS_HIDDEN_(void) Assign(char_type aChar)
  889.   {
  890.     NS_StringSetData(*this, &aChar, 1);
  891.   }
  892.  
  893.   NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString);   return *this; }
  894.   NS_HIDDEN_(self_type&) operator=(const char_type* aPtr)    { Assign(aPtr);      return *this; }
  895.   NS_HIDDEN_(self_type&) operator=(char_type aChar)          { Assign(aChar);     return *this; }
  896.  
  897.   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) )
  898.   {
  899.     NS_StringSetDataRange(*this, cutStart, cutLength, data, length);
  900.   }
  901.   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c )
  902.   {
  903.     Replace(cutStart, cutLength, &c, 1);
  904.   }
  905.   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable )
  906.   {
  907.     const char_type* data;
  908.     PRUint32 dataLen = NS_StringGetData(readable, &data);
  909.     NS_StringSetDataRange(*this, cutStart, cutLength, data, dataLen);
  910.   }
  911.  
  912.   NS_HIDDEN_(void) Append( char_type c )                                                              { Replace(size_type(-1), 0, c); }
  913.   NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) )                  { Replace(size_type(-1), 0, data, length); }
  914.   NS_HIDDEN_(void) Append( const self_type& readable )                                                { Replace(size_type(-1), 0, readable); }
  915.  
  916.   NS_HIDDEN_(self_type&) operator+=( char_type c )                                                    { Append(c);        return *this; }
  917.   NS_HIDDEN_(self_type&) operator+=( const char_type* data )                                          { Append(data);     return *this; }
  918.   NS_HIDDEN_(self_type&) operator+=( const self_type& readable )                                      { Append(readable); return *this; }
  919.  
  920.   NS_HIDDEN_(void) Insert( char_type c, index_type pos )                                              { Replace(pos, 0, c); }
  921.   NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) )  { Replace(pos, 0, data, length); }
  922.   NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos )                                { Replace(pos, 0, readable); }
  923.  
  924.   NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength )                                    { Replace(cutStart, cutLength, nsnull, 0); }
  925.  
  926.   NS_HIDDEN_(PRBool) Equals( const self_type &other ) const {
  927.     const char_type *cself;
  928.     const char_type *cother;
  929.     PRUint32 selflen = NS_StringGetData(*this, &cself);
  930.     PRUint32 otherlen = NS_StringGetData(other, &cother);
  931.  
  932.     if (selflen != otherlen)
  933.       return PR_FALSE;
  934.  
  935.     return memcmp(cself, cother, selflen * sizeof(char_type)) == 0;
  936.   }
  937.  
  938. #endif // MOZILLA_INTERNAL_API
  939.  
  940. protected:
  941.   // Prevent people from allocating a nsAString directly.
  942.   ~nsAString_external() {}
  943.  
  944. private:
  945.   void *v;
  946. };
  947.  
  948. class nsACString_external
  949. {
  950. #ifndef MOZILLA_INTERNAL_API
  951.  
  952. public:
  953.   typedef char                  char_type;
  954.   typedef nsACString_external   self_type;
  955.   typedef PRUint32              size_type;
  956.   typedef PRUint32              index_type;
  957.  
  958.   NS_HIDDEN_(const char_type*) BeginReading() const
  959.   {
  960.     const char_type *data;
  961.     NS_CStringGetData(*this, &data);
  962.     return data;
  963.   }
  964.  
  965.   NS_HIDDEN_(const char_type*) EndReading() const
  966.   {
  967.     const char_type *data;
  968.     PRUint32 len = NS_CStringGetData(*this, &data);
  969.     return data + len;
  970.   }
  971.  
  972.   NS_HIDDEN_(char_type*) BeginWriting()
  973.   {
  974.     char_type *data;
  975.     NS_CStringGetMutableData(*this, PR_UINT32_MAX, &data);
  976.     return data;
  977.   }
  978.  
  979.   NS_HIDDEN_(PRBool) SetLength(PRUint32 aLen)
  980.   {
  981.     char_type *data;
  982.     NS_CStringGetMutableData(*this, aLen, &data);
  983.     return data != nsnull;
  984.   }
  985.  
  986.   NS_HIDDEN_(size_type) Length() const
  987.   {
  988.     const char_type* data;
  989.     return NS_CStringGetData(*this, &data);
  990.   }
  991.  
  992.   NS_HIDDEN_(PRBool) IsEmpty() const
  993.   {
  994.     return Length() == 0;
  995.   }
  996.  
  997.   NS_HIDDEN_(void) Assign(const self_type& aString)
  998.   {
  999.     NS_CStringCopy(*this, aString);
  1000.   }
  1001.   NS_HIDDEN_(void) Assign(const char_type* aData, size_type aLength = PR_UINT32_MAX)
  1002.   {
  1003.     NS_CStringSetData(*this, aData, aLength);
  1004.   }
  1005.   NS_HIDDEN_(void) Assign(char_type aChar)
  1006.   {
  1007.     NS_CStringSetData(*this, &aChar, 1);
  1008.   }
  1009.  
  1010.   NS_HIDDEN_(self_type&) operator=(const self_type& aString) { Assign(aString);   return *this; }
  1011.   NS_HIDDEN_(self_type&) operator=(const char_type* aPtr)    { Assign(aPtr);      return *this; }
  1012.   NS_HIDDEN_(self_type&) operator=(char_type aChar)          { Assign(aChar);     return *this; }
  1013.  
  1014.   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) )
  1015.   {
  1016.     NS_CStringSetDataRange(*this, cutStart, cutLength, data, length);
  1017.   }
  1018.   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, char_type c )
  1019.   {
  1020.     Replace(cutStart, cutLength, &c, 1);
  1021.   }
  1022.   NS_HIDDEN_(void) Replace( index_type cutStart, size_type cutLength, const self_type& readable )
  1023.   {
  1024.     const char_type* data;
  1025.     PRUint32 dataLen = NS_CStringGetData(readable, &data);
  1026.     NS_CStringSetDataRange(*this, cutStart, cutLength, data, dataLen);
  1027.   }
  1028.  
  1029.   NS_HIDDEN_(void) Append( char_type c )                                                              { Replace(size_type(-1), 0, c); }
  1030.   NS_HIDDEN_(void) Append( const char_type* data, size_type length = size_type(-1) )                  { Replace(size_type(-1), 0, data, length); }
  1031.   NS_HIDDEN_(void) Append( const self_type& readable )                                                { Replace(size_type(-1), 0, readable); }
  1032.  
  1033.   NS_HIDDEN_(self_type&) operator+=( char_type c )                                                    { Append(c);        return *this; }
  1034.   NS_HIDDEN_(self_type&) operator+=( const char_type* data )                                          { Append(data);     return *this; }
  1035.   NS_HIDDEN_(self_type&) operator+=( const self_type& readable )                                      { Append(readable); return *this; }
  1036.  
  1037.   NS_HIDDEN_(void) Insert( char_type c, index_type pos )                                              { Replace(pos, 0, c); }
  1038.   NS_HIDDEN_(void) Insert( const char_type* data, index_type pos, size_type length = size_type(-1) )  { Replace(pos, 0, data, length); }
  1039.   NS_HIDDEN_(void) Insert( const self_type& readable, index_type pos )                                { Replace(pos, 0, readable); }
  1040.  
  1041.   NS_HIDDEN_(void) Cut( index_type cutStart, size_type cutLength )                                    { Replace(cutStart, cutLength, nsnull, 0); }
  1042.  
  1043.   NS_HIDDEN_(PRBool) Equals( const self_type &other ) const {
  1044.     const char_type *cself;
  1045.     const char_type *cother;
  1046.     PRUint32 selflen = NS_CStringGetData(*this, &cself);
  1047.     PRUint32 otherlen = NS_CStringGetData(other, &cother);
  1048.  
  1049.     if (selflen != otherlen)
  1050.       return PR_FALSE;
  1051.  
  1052.     return memcmp(cself, cother, selflen * sizeof(char_type)) == 0;
  1053.   }
  1054.  
  1055. #endif // MOZILLA_INTERNAL_API
  1056.  
  1057. protected:
  1058.   // Prevent people from allocating a nsACString directly.
  1059.   ~nsACString_external() {}
  1060.  
  1061. private:
  1062.   void *v;
  1063. };
  1064.  
  1065. /* ------------------------------------------------------------------------- */
  1066.  
  1067. /**
  1068.  * Below we define nsStringContainer and nsCStringContainer.  These classes
  1069.  * have unspecified structure.  In most cases, your code should use
  1070.  * nsEmbedString instead of these classes; however, if you prefer C-style
  1071.  * programming, then look no further...
  1072.  */
  1073.  
  1074. class nsStringContainer : public nsAString_external
  1075. {
  1076. private:
  1077.   void     *d1;
  1078.   PRUint32  d2;
  1079.   void     *d3;
  1080.  
  1081. public:
  1082.   nsStringContainer() {} // MSVC6 needs this
  1083. };
  1084.  
  1085. class nsCStringContainer : public nsACString_external
  1086. {
  1087. private:
  1088.   void    *d1;
  1089.   PRUint32 d2;
  1090.   void    *d3;
  1091.  
  1092. public:
  1093.   nsCStringContainer() {} // MSVC6 needs this
  1094. };
  1095.  
  1096. /* ------------------------------------------------------------------------- */
  1097.  
  1098. /**
  1099.  * Below we define a number of inlined helper classes that make the frozen
  1100.  * string API easier to use.
  1101.  */
  1102.  
  1103. #ifndef MOZILLA_INTERNAL_API
  1104. #include "nsDebug.h"
  1105.  
  1106. /**
  1107.  * Rename symbols to avoid conflicting with internal versions.
  1108.  */
  1109. #define nsString                       nsString_external
  1110. #define nsCString                      nsCString_external
  1111. #define nsDependentString              nsDependentString_external
  1112. #define nsDependentCString             nsDependentCString_external
  1113. #define NS_ConvertASCIItoUTF16         NS_ConvertASCIItoUTF16_external
  1114. #define NS_ConvertUTF8toUTF16          NS_ConvertUTF8toUTF16_external
  1115. #define NS_ConvertUTF16toUTF8          NS_ConvertUTF16toUTF8_external
  1116. #define NS_LossyConvertUTF16toASCII    NS_LossyConvertUTF16toASCII_external
  1117. #define nsGetterCopies                 nsGetterCopies_external
  1118. #define nsCGetterCopies                nsCGetterCopies_external
  1119. #define nsDependentSubstring           nsDependentSubstring_external
  1120. #define nsDependentCSubstring          nsDependentCSubstring_external
  1121.  
  1122. /**
  1123.  * basic strings
  1124.  */
  1125.  
  1126. class nsString : public nsStringContainer
  1127. {
  1128. public:
  1129.   typedef nsString         self_type;
  1130.   typedef nsAString        abstract_string_type;
  1131.  
  1132.   nsString()
  1133.   {
  1134.     NS_StringContainerInit(*this);
  1135.   }
  1136.  
  1137.   nsString(const self_type& aString)
  1138.   {
  1139.     NS_StringContainerInit(*this);
  1140.     NS_StringCopy(*this, aString);
  1141.   }
  1142.  
  1143.   explicit
  1144.   nsString(const abstract_string_type& aReadable)
  1145.   {
  1146.     NS_StringContainerInit(*this);
  1147.     NS_StringCopy(*this, aReadable);
  1148.   }
  1149.  
  1150.   explicit
  1151.   nsString(const char_type* aData, size_type aLength = PR_UINT32_MAX)
  1152.   {
  1153.     NS_StringContainerInit2(*this, aData, aLength, 0);
  1154.   }
  1155.   
  1156.   ~nsString()
  1157.   {
  1158.     NS_StringContainerFinish(*this);
  1159.   }
  1160.  
  1161.   const char_type* get() const
  1162.   {
  1163.     const char_type* data;
  1164.     NS_StringGetData(*this, &data);
  1165.     return data;
  1166.   }
  1167.   
  1168.   self_type& operator=(const self_type& aString)              { Assign(aString);   return *this; }
  1169.   self_type& operator=(const abstract_string_type& aReadable) { Assign(aReadable); return *this; }
  1170.   self_type& operator=(const char_type* aPtr)                 { Assign(aPtr);      return *this; }
  1171.   self_type& operator=(char_type aChar)                       { Assign(aChar);     return *this; }
  1172.  
  1173.   void Adopt(const char_type *aData, size_type aLength = PR_UINT32_MAX)
  1174.   {
  1175.     NS_StringContainerFinish(*this);
  1176.     NS_StringContainerInit2(*this, aData, aLength,
  1177.                             NS_STRING_CONTAINER_INIT_ADOPT);
  1178.   }
  1179.  
  1180. protected:
  1181.   
  1182.   nsString(const char_type* aData, size_type aLength, PRUint32 aFlags)
  1183.   {
  1184.     NS_StringContainerInit2(*this, aData, aLength, aFlags);
  1185.   }
  1186. };
  1187.  
  1188. class nsCString : public nsCStringContainer
  1189. {
  1190. public:
  1191.   typedef nsCString        self_type;
  1192.   typedef nsACString       abstract_string_type;
  1193.  
  1194.   nsCString()
  1195.   {
  1196.     NS_CStringContainerInit(*this);
  1197.   }
  1198.  
  1199.   nsCString(const self_type& aString)
  1200.   {
  1201.     NS_CStringContainerInit(*this);
  1202.     NS_CStringCopy(*this, aString);
  1203.   }
  1204.  
  1205.   explicit
  1206.   nsCString(const abstract_string_type& aReadable)
  1207.   {
  1208.     NS_CStringContainerInit(*this);
  1209.     NS_CStringCopy(*this, aReadable);
  1210.   }
  1211.  
  1212.   explicit
  1213.   nsCString(const char_type* aData, size_type aLength = PR_UINT32_MAX)
  1214.   {
  1215.     NS_CStringContainerInit(*this);
  1216.     NS_CStringSetData(*this, aData, aLength);
  1217.   }
  1218.   
  1219.   ~nsCString()
  1220.   {
  1221.     NS_CStringContainerFinish(*this);
  1222.   }
  1223.  
  1224.   const char_type* get() const
  1225.   {
  1226.     const char_type* data;
  1227.     NS_CStringGetData(*this, &data);
  1228.     return data;
  1229.   }
  1230.   
  1231.   self_type& operator=(const self_type& aString)              { Assign(aString);   return *this; }
  1232.   self_type& operator=(const abstract_string_type& aReadable) { Assign(aReadable); return *this; }
  1233.   self_type& operator=(const char_type* aPtr)                 { Assign(aPtr);      return *this; }
  1234.   self_type& operator=(char_type aChar)                       { Assign(aChar);     return *this; }
  1235.  
  1236.   void Adopt(const char_type *aData, size_type aLength = PR_UINT32_MAX)
  1237.   {
  1238.     NS_CStringContainerFinish(*this);
  1239.     NS_CStringContainerInit2(*this, aData, aLength,
  1240.                              NS_CSTRING_CONTAINER_INIT_ADOPT);
  1241.   }
  1242.  
  1243. protected:
  1244.   
  1245.   nsCString(const char_type* aData, size_type aLength, PRUint32 aFlags)
  1246.   {
  1247.     NS_CStringContainerInit2(*this, aData, aLength, aFlags);
  1248.   }
  1249. };
  1250.  
  1251.  
  1252. /**
  1253.  * dependent strings
  1254.  */
  1255.  
  1256. class nsDependentString : public nsString
  1257. {
  1258. public:
  1259.   typedef nsDependentString         self_type;
  1260.  
  1261.   nsDependentString() {}
  1262.  
  1263.   explicit
  1264.   nsDependentString(const char_type* aData, size_type aLength = PR_UINT32_MAX)
  1265.     : nsString(aData, aLength, NS_CSTRING_CONTAINER_INIT_DEPEND)
  1266.   {}
  1267.  
  1268.   void Rebind(const char_type* aData, size_type aLength = PR_UINT32_MAX)
  1269.   {
  1270.     NS_StringContainerFinish(*this);
  1271.     NS_StringContainerInit2(*this, aData, aLength,
  1272.                             NS_STRING_CONTAINER_INIT_DEPEND);
  1273.   }
  1274.   
  1275. private:
  1276.   self_type& operator=(const self_type& aString); // NOT IMPLEMENTED
  1277. };
  1278.  
  1279. class nsDependentCString : public nsCString
  1280. {
  1281. public:
  1282.   typedef nsDependentCString        self_type;
  1283.  
  1284.   nsDependentCString() {}
  1285.  
  1286.   explicit
  1287.   nsDependentCString(const char_type* aData, size_type aLength = PR_UINT32_MAX)
  1288.     : nsCString(aData, aLength, NS_CSTRING_CONTAINER_INIT_DEPEND)
  1289.   {}
  1290.  
  1291.   void Rebind(const char_type* aData, size_type aLength = PR_UINT32_MAX)
  1292.   {
  1293.     NS_CStringContainerFinish(*this);
  1294.     NS_CStringContainerInit2(*this, aData, aLength,
  1295.                              NS_CSTRING_CONTAINER_INIT_DEPEND);
  1296.   }
  1297.   
  1298. private:
  1299.   self_type& operator=(const self_type& aString); // NOT IMPLEMENTED
  1300. };
  1301.  
  1302.  
  1303. /**
  1304.  * conversion classes
  1305.  */
  1306.  
  1307. class NS_ConvertASCIItoUTF16 : public nsString
  1308. {
  1309. public:
  1310.   typedef NS_ConvertASCIItoUTF16    self_type;
  1311.  
  1312.   explicit
  1313.   NS_ConvertASCIItoUTF16(const nsACString& aStr)
  1314.   {
  1315.     NS_CStringToUTF16(aStr, NS_CSTRING_ENCODING_ASCII, *this);
  1316.   }
  1317.  
  1318.   explicit
  1319.   NS_ConvertASCIItoUTF16(const char* aData, PRUint32 aLength = PR_UINT32_MAX)
  1320.   {
  1321.     NS_CStringToUTF16(nsDependentCString(aData, aLength),
  1322.                       NS_CSTRING_ENCODING_ASCII, *this);
  1323.   }
  1324.  
  1325. private:
  1326.   self_type& operator=(const self_type& aString); // NOT IMPLEMENTED
  1327. };
  1328.  
  1329. class NS_ConvertUTF8toUTF16 : public nsString
  1330. {
  1331. public:
  1332.   typedef NS_ConvertUTF8toUTF16    self_type;
  1333.  
  1334.   explicit
  1335.   NS_ConvertUTF8toUTF16(const nsACString& aStr)
  1336.   {
  1337.     NS_CStringToUTF16(aStr, NS_CSTRING_ENCODING_UTF8, *this);
  1338.   }
  1339.  
  1340.   explicit
  1341.   NS_ConvertUTF8toUTF16(const char* aData, PRUint32 aLength = PR_UINT32_MAX)
  1342.   {
  1343.     NS_CStringToUTF16(nsDependentCString(aData, aLength),
  1344.                       NS_CSTRING_ENCODING_UTF8, *this);
  1345.   }
  1346.  
  1347. private:
  1348.   self_type& operator=(const self_type& aString); // NOT IMPLEMENTED
  1349. };
  1350.  
  1351. class NS_ConvertUTF16toUTF8 : public nsCString
  1352. {
  1353. public:
  1354.   typedef NS_ConvertUTF16toUTF8    self_type;
  1355.  
  1356.   explicit
  1357.   NS_ConvertUTF16toUTF8(const nsAString& aStr)
  1358.   {
  1359.     NS_UTF16ToCString(aStr, NS_CSTRING_ENCODING_UTF8, *this);
  1360.   }
  1361.  
  1362.   explicit
  1363.   NS_ConvertUTF16toUTF8(const PRUnichar* aData, PRUint32 aLength = PR_UINT32_MAX)
  1364.   {
  1365.     NS_UTF16ToCString(nsDependentString(aData, aLength),
  1366.                       NS_CSTRING_ENCODING_UTF8, *this);
  1367.   }
  1368.  
  1369. private:
  1370.   self_type& operator=(const self_type& aString); // NOT IMPLEMENTED
  1371. };
  1372.  
  1373. class NS_LossyConvertUTF16toASCII : public nsCString
  1374. {
  1375. public:
  1376.   typedef NS_LossyConvertUTF16toASCII    self_type;
  1377.  
  1378.   explicit
  1379.   NS_LossyConvertUTF16toASCII(const nsAString& aStr)
  1380.   {
  1381.     NS_UTF16ToCString(aStr, NS_CSTRING_ENCODING_ASCII, *this);
  1382.   }
  1383.  
  1384.   explicit
  1385.   NS_LossyConvertUTF16toASCII(const PRUnichar* aData, PRUint32 aLength = PR_UINT32_MAX)
  1386.   {
  1387.     NS_UTF16ToCString(nsDependentString(aData, aLength),
  1388.                       NS_CSTRING_ENCODING_ASCII, *this);
  1389.   }
  1390.  
  1391. private:
  1392.   self_type& operator=(const self_type& aString); // NOT IMPLEMENTED
  1393. };
  1394.  
  1395.  
  1396. /**
  1397.  * literal strings
  1398.  *
  1399.  * NOTE: HAVE_CPP_2BYTE_WCHAR_T may be automatically defined for some platforms
  1400.  * in nscore.h.  On other platforms, it may be defined in xpcom-config.h.
  1401.  * Under GCC, this define should only be set if compiling with -fshort-wchar.
  1402.  */
  1403.  
  1404. #ifdef HAVE_CPP_2BYTE_WCHAR_T
  1405.   #define NS_LL(s)                                L##s
  1406.   #define NS_MULTILINE_LITERAL_STRING(s)          nsDependentString(NS_REINTERPRET_CAST(const nsAString::char_type*, s), PRUint32((sizeof(s)/sizeof(wchar_t))-1))
  1407.   #define NS_MULTILINE_LITERAL_STRING_INIT(n,s)   n(NS_REINTERPRET_CAST(const nsAString::char_type*, s), PRUint32((sizeof(s)/sizeof(wchar_t))-1))
  1408.   #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s)  const nsDependentString n(NS_REINTERPRET_CAST(const nsAString::char_type*, s), PRUint32((sizeof(s)/sizeof(wchar_t))-1))
  1409.   typedef nsDependentString nsLiteralString;
  1410. #else
  1411.   #define NS_LL(s)                                s
  1412.   #define NS_MULTILINE_LITERAL_STRING(s)          NS_ConvertASCIItoUTF16(s, PRUint32(sizeof(s)-1))
  1413.   #define NS_MULTILINE_LITERAL_STRING_INIT(n,s)   n(s, PRUint32(sizeof(s)-1))
  1414.   #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s)  const NS_ConvertASCIItoUTF16 n(s, PRUint32(sizeof(s)-1))
  1415.   typedef NS_ConvertASCIItoUTF16 nsLiteralString;
  1416. #endif
  1417.  
  1418. /*
  1419.  * Macro arguments used in concatenation or stringification won't be expanded.
  1420.  * Therefore, in order for |NS_L(FOO)| to work as expected (which is to expand
  1421.  * |FOO| before doing whatever |NS_L| needs to do to it) a helper macro needs
  1422.  * to be inserted in between to allow the macro argument to expand.
  1423.  * See "3.10.6 Separate Expansion of Macro Arguments" of the CPP manual for a
  1424.  * more accurate and precise explanation.
  1425.  */
  1426.  
  1427. #define NS_L(s)                                   NS_LL(s)
  1428.  
  1429. #define NS_LITERAL_STRING(s)                      NS_STATIC_CAST(const nsString&, NS_MULTILINE_LITERAL_STRING(NS_LL(s)))
  1430. #define NS_LITERAL_STRING_INIT(n,s)               NS_MULTILINE_LITERAL_STRING_INIT(n, NS_LL(s))
  1431. #define NS_NAMED_LITERAL_STRING(n,s)              NS_NAMED_MULTILINE_LITERAL_STRING(n, NS_LL(s))
  1432.  
  1433. #define NS_LITERAL_CSTRING(s)                     NS_STATIC_CAST(const nsDependentCString&, nsDependentCString(s, PRUint32(sizeof(s)-1)))
  1434. #define NS_LITERAL_CSTRING_INIT(n,s)              n(s, PRUint32(sizeof(s)-1))
  1435. #define NS_NAMED_LITERAL_CSTRING(n,s)             const nsDependentCString n(s, PRUint32(sizeof(s)-1))
  1436.  
  1437. typedef nsDependentCString nsLiteralCString;
  1438.  
  1439.  
  1440. /**
  1441.  * getter_Copies support
  1442.  *
  1443.  *    NS_IMETHOD GetBlah(PRUnichar**);
  1444.  *
  1445.  *    void some_function()
  1446.  *    {
  1447.  *      nsString blah;
  1448.  *      GetBlah(getter_Copies(blah));
  1449.  *      // ...
  1450.  *    }
  1451.  */
  1452.  
  1453. class nsGetterCopies
  1454. {
  1455. public:
  1456.   typedef PRUnichar char_type;
  1457.  
  1458.   nsGetterCopies(nsString& aStr)
  1459.     : mString(aStr), mData(nsnull)
  1460.   {}
  1461.  
  1462.   ~nsGetterCopies()
  1463.   {
  1464.     mString.Adopt(mData);
  1465.   }
  1466.  
  1467.   operator char_type**()
  1468.   {
  1469.     return &mData;
  1470.   }
  1471.  
  1472. private:
  1473.   nsString&  mString;
  1474.   char_type* mData;
  1475. };
  1476.  
  1477. inline nsGetterCopies
  1478. getter_Copies(nsString& aString)
  1479. {
  1480.   return nsGetterCopies(aString);
  1481. }
  1482.  
  1483. class nsCGetterCopies
  1484. {
  1485. public:
  1486.   typedef char char_type;
  1487.  
  1488.   nsCGetterCopies(nsCString& aStr)
  1489.     : mString(aStr), mData(nsnull)
  1490.   {}
  1491.  
  1492.   ~nsCGetterCopies()
  1493.   {
  1494.     mString.Adopt(mData);
  1495.   }
  1496.  
  1497.   operator char_type**()
  1498.   {
  1499.     return &mData;
  1500.   }
  1501.  
  1502. private:
  1503.   nsCString& mString;
  1504.   char_type* mData;
  1505. };
  1506.  
  1507. inline nsCGetterCopies
  1508. getter_Copies(nsCString& aString)
  1509. {
  1510.   return nsCGetterCopies(aString);
  1511. }
  1512.  
  1513.  
  1514. /**
  1515. * substrings
  1516. */
  1517.  
  1518. class nsDependentSubstring : public nsStringContainer
  1519. {
  1520. public:
  1521.   typedef nsDependentSubstring self_type;
  1522.   typedef nsAString            abstract_string_type;
  1523.  
  1524.   ~nsDependentSubstring()
  1525.   {
  1526.     NS_StringContainerFinish(*this);
  1527.   }
  1528.  
  1529.   nsDependentSubstring()
  1530.   {
  1531.     NS_StringContainerInit(*this);
  1532.   }
  1533.  
  1534.   nsDependentSubstring(const char_type *aStart, PRUint32 aLength)
  1535.   {
  1536.     NS_StringContainerInit2(*this, aStart, aLength,
  1537.                             NS_STRING_CONTAINER_INIT_DEPEND |
  1538.                             NS_STRING_CONTAINER_INIT_SUBSTRING);
  1539.   }
  1540.  
  1541.   nsDependentSubstring(const abstract_string_type& aStr,
  1542.                        PRUint32 aStartPos)
  1543.   {
  1544.     const PRUnichar* data;
  1545.     PRUint32 len = NS_StringGetData(aStr, &data);
  1546.     NS_StringContainerInit2(*this, data + aStartPos, len - aStartPos,
  1547.                             NS_STRING_CONTAINER_INIT_DEPEND |
  1548.                             NS_STRING_CONTAINER_INIT_SUBSTRING);
  1549.   }
  1550.  
  1551.   nsDependentSubstring(const abstract_string_type& aStr,
  1552.                        PRUint32 aStartPos, PRUint32 aLength)
  1553.   {
  1554.     const PRUnichar* data;
  1555. #ifdef DEBUG
  1556.     PRUint32 len =
  1557. #endif
  1558.     NS_StringGetData(aStr, &data);
  1559.     NS_ASSERTION(aStartPos + aLength <= len, "bad length");
  1560.     NS_StringContainerInit2(*this, data + aStartPos, aLength,
  1561.                             NS_STRING_CONTAINER_INIT_DEPEND |
  1562.                             NS_STRING_CONTAINER_INIT_SUBSTRING);
  1563.   }
  1564.  
  1565.   void Rebind(const char_type *aStart, PRUint32 aLength)
  1566.   {
  1567.     NS_StringContainerFinish(*this);
  1568.     NS_StringContainerInit2(*this, aStart, aLength,
  1569.                             NS_STRING_CONTAINER_INIT_DEPEND |
  1570.                             NS_STRING_CONTAINER_INIT_SUBSTRING);
  1571.   }
  1572.  
  1573. private:
  1574.   self_type& operator=(const self_type& aString); // NOT IMPLEMENTED
  1575. };
  1576.  
  1577. class nsDependentCSubstring : public nsCStringContainer
  1578. {
  1579. public:
  1580.   typedef nsDependentCSubstring self_type;
  1581.   typedef nsACString            abstract_string_type;
  1582.  
  1583.   ~nsDependentCSubstring()
  1584.   {
  1585.     NS_CStringContainerFinish(*this);
  1586.   }
  1587.  
  1588.   nsDependentCSubstring()
  1589.   {
  1590.     NS_CStringContainerInit(*this);
  1591.   }
  1592.  
  1593.   nsDependentCSubstring(const char_type *aStart, PRUint32 aLength)
  1594.   {
  1595.     NS_CStringContainerInit2(*this, aStart, aLength,
  1596.                              NS_CSTRING_CONTAINER_INIT_DEPEND |
  1597.                              NS_CSTRING_CONTAINER_INIT_SUBSTRING);
  1598.   }
  1599.  
  1600.   nsDependentCSubstring(const abstract_string_type& aStr,
  1601.                         PRUint32 aStartPos)
  1602.   {
  1603.     const char* data;
  1604.     PRUint32 len = NS_CStringGetData(aStr, &data);
  1605.     NS_CStringContainerInit2(*this, data + aStartPos, len - aStartPos,
  1606.                              NS_CSTRING_CONTAINER_INIT_DEPEND |
  1607.                              NS_CSTRING_CONTAINER_INIT_SUBSTRING);
  1608.   }
  1609.  
  1610.   nsDependentCSubstring(const abstract_string_type& aStr,
  1611.                         PRUint32 aStartPos, PRUint32 aLength)
  1612.   {
  1613.     const char* data;
  1614. #ifdef DEBUG
  1615.     PRUint32 len =
  1616. #endif
  1617.     NS_CStringGetData(aStr, &data);
  1618.     NS_ASSERTION(aStartPos + aLength <= len, "bad length");
  1619.     NS_CStringContainerInit2(*this, data + aStartPos, aLength,
  1620.                              NS_CSTRING_CONTAINER_INIT_DEPEND |
  1621.                              NS_CSTRING_CONTAINER_INIT_SUBSTRING);
  1622.   }
  1623.  
  1624.   void Rebind(const char_type *aStart, PRUint32 aLength)
  1625.   {
  1626.     NS_CStringContainerFinish(*this);
  1627.     NS_CStringContainerInit2(*this, aStart, aLength,
  1628.                              NS_CSTRING_CONTAINER_INIT_DEPEND |
  1629.                              NS_CSTRING_CONTAINER_INIT_SUBSTRING);
  1630.   }
  1631.  
  1632. private:
  1633.   self_type& operator=(const self_type& aString); // NOT IMPLEMENTED
  1634. };
  1635.  
  1636.  
  1637. /**
  1638.  * Various nsDependentC?Substring constructor functions
  1639.  */
  1640.  
  1641. // PRUnichar
  1642. inline
  1643. const nsDependentSubstring
  1644. Substring( const nsAString& str, PRUint32 startPos )
  1645. {
  1646.   return nsDependentSubstring(str, startPos);
  1647. }
  1648.  
  1649. inline
  1650. const nsDependentSubstring
  1651. Substring( const nsAString& str, PRUint32 startPos, PRUint32 length )
  1652. {
  1653.   return nsDependentSubstring(str, startPos, length);
  1654. }
  1655.  
  1656. inline
  1657. const nsDependentSubstring
  1658. Substring( const PRUnichar* start, const PRUnichar* end )
  1659. {
  1660.   return nsDependentSubstring(start, end - start);
  1661. }
  1662.  
  1663. inline
  1664. const nsDependentSubstring
  1665. Substring( const PRUnichar* start, PRUint32 length )
  1666. {
  1667.   return nsDependentSubstring(start, length);
  1668. }
  1669.  
  1670. inline
  1671. const nsDependentSubstring
  1672. StringHead( const nsAString& str, PRUint32 count )
  1673. {
  1674.   return nsDependentSubstring(str, 0, count);
  1675. }
  1676.  
  1677. inline
  1678. const nsDependentSubstring
  1679. StringTail( const nsAString& str, PRUint32 count )
  1680. {
  1681.   return nsDependentSubstring(str, str.Length() - count, count);
  1682. }
  1683.  
  1684. // char
  1685. inline
  1686. const nsDependentCSubstring
  1687. Substring( const nsACString& str, PRUint32 startPos )
  1688. {
  1689.   return nsDependentCSubstring(str, startPos);
  1690. }
  1691.  
  1692. inline
  1693. const nsDependentCSubstring
  1694. Substring( const nsACString& str, PRUint32 startPos, PRUint32 length )
  1695. {
  1696.   return nsDependentCSubstring(str, startPos, length);
  1697. }
  1698.  
  1699. inline
  1700. const nsDependentCSubstring
  1701. Substring( const char* start, const char* end )
  1702. {
  1703.   return nsDependentCSubstring(start, end - start);
  1704. }
  1705.  
  1706. inline
  1707. const nsDependentCSubstring
  1708. Substring( const char* start, PRUint32 length )
  1709. {
  1710.   return nsDependentCSubstring(start, length);
  1711. }
  1712.  
  1713. inline
  1714. const nsDependentCSubstring
  1715. StringHead( const nsACString& str, PRUint32 count )
  1716. {
  1717.   return nsDependentCSubstring(str, 0, count);
  1718. }
  1719.  
  1720. inline
  1721. const nsDependentCSubstring
  1722. StringTail( const nsACString& str, PRUint32 count )
  1723. {
  1724.   return nsDependentCSubstring(str, str.Length() - count, count);
  1725. }
  1726.  
  1727.  
  1728. /*
  1729.  * Canonical empty strings
  1730.  */
  1731.  
  1732. #define EmptyCString() nsCString()
  1733. #define EmptyString() nsString()
  1734.  
  1735. #endif // MOZILLA_INTERNAL_API
  1736.  
  1737. #endif // nsStringAPI_h__
  1738.